package com.lunwen.view;

import com.lunwen.domain.User;
import com.lunwen.view.component.Context;
import com.lunwen.view.component.MeFrame;

import javax.swing.*;
import java.awt.event.ActionEvent;

public class RootFrame extends MeFrame {


    private JButton rsg = new JButton("新增论文");
    private JButton update = new JButton("修改论文");
    private JButton del = new JButton("删除论文");
    private JButton replace = new JButton("检索论文");
    private JButton append = new JButton("学生信息录入");
    private JButton index = new JButton("学生信息检索");


    private Context right = null;

    private MeControl meControl = null;

    public RootFrame(LoginFrame loginFrame, User user) {
        super("管理员" , 1000, 600);

        meControl = new MeControl(loginFrame,this);

        meControl.setName(user.getName());

        meControl.setLocation(850,100);
        context.add(meControl);

        //初始化左边
        context.add(rsg,20);
        rsg.setBounds(10,60,180,50);

        context.add(update,20);
        update.setBounds(10,130,180,50);

        context.add(del,20);
        del.setBounds(10,200,180,50);

        context.add(replace,20);
        replace.setBounds(10,270,180,50);

        context.add(append,20);
        append.setBounds(10,340,180,50);

        context.add(index,20);
        index.setBounds(10,410,180,50);


        // 左边添加监听器
        rsg.addActionListener(this);
        update.addActionListener(this);
        replace.addActionListener(this);
        del.addActionListener(this);
        append.addActionListener(this);
        index.addActionListener(this);
    }

    @Override
    public void doActionPerformed(ActionEvent e) {
        if (right != null){
            context.remove(right);
        }
        if (e.getSource() == rsg){
            //添加论文
            right = new ArticleFormFrame(this.getWidth()-400,this.getHeight()+100);
            context.add(right);
            right.setLocation(200,60);
            this.setState(JFrame.HIDE_ON_CLOSE);
        }else if (e.getSource() == update){
            //修改论文
            right = new UpdateArticleFrame(this.getWidth()-400,this.getHeight()+100);
            context.add(right);
            right.setLocation(200,60);
        }else if (e.getSource() == del){
            //删除论文
            right = new DeleteArticleFrame(this.getWidth()-400,this.getHeight()+100);
            context.add(right);
            right.setLocation(200,60);
        }else if (e.getSource() == append){
            //新增学生
            right = new StudentFormFrame(this.getWidth()-400,this.getHeight()+100);
            context.add(right);
            right.setLocation(200,60);
        }else if (e.getSource() == index){
            //检索学生
            right = new SearchStudentFrame(this.getWidth()-400,this.getHeight()+100);
            context.add(right);
            right.setLocation(200,60);
        }else if (e.getSource() == replace){
            //检索论文
            right = new ArticleFrame(this.getWidth()-400,this.getHeight()+100);
            context.add(right);
            right.setLocation(200,60);
        }

    }
}